Imports DaveSexton.DocProject.DocSites
Imports DaveSexton.DocProject.DocSites.Configuration

Partial Public Class DocSiteAdmin
	Inherits System.Web.UI.Page

	Public Shared Function GetStatisticsForBinding() As Object
		Return New SearchIndexStatistics()
	End Function

	Public Shared Function GetSettingsForBinding() As DocSiteSettings
		Return DocSiteManager.Settings
	End Function

	Public Shared Sub UpdateSettings(ByVal newSettings As DocSiteSettings)
		Dim settings As DocSiteSettings = DocSiteManager.Settings

		settings.Update(newSettings)
		settings.Save()
	End Sub

	Protected Function EnsureAbsolutePath(ByVal path As String) As String
		If VirtualPathUtility.IsAppRelative(path) Then
			Return VirtualPathUtility.ToAbsolute(path)
		Else
			Return path
		End If
	End Function

	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
		ScriptManager.GetCurrent(Page).AsyncPostBackTimeout = DocSiteManager.Settings.CreateIndexRefreshTimeout

		MyBase.OnInit(e)
	End Sub

	Protected Sub createIndexLinkButton_Click(ByVal sender As Object, ByVal e As EventArgs)
		DaveSexton.DocProject.DocSites.DocSiteSearch.CreateSearchIndex()

		searchStatsDetailsView.DataBind()
		searchStatsUpdatePanel.Update()
	End Sub

	Protected Sub searchSettingsDetailsView_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
		e.NewValues("SearchPath") = CType(searchSettingsDetailsView.FindControl("searchPathTextBox"), TextBox).Text
	End Sub

	Protected Sub searchWeightFactorsDetailsView_ItemUpdating(ByVal sender As Object, ByVal e As DetailsViewUpdateEventArgs)
		e.NewValues("SearchEarlyKeywordWeightFactor") = CType(searchWeightFactorsDetailsView.FindControl("earlyKeywordWeightFactorTextBox"), TextBox).Text
		e.NewValues("SearchTitleKeywordWeightFactor") = CType(searchWeightFactorsDetailsView.FindControl("titleKeywordWeightFactorTextBox"), TextBox).Text
	End Sub

	Private NotInheritable Class SearchIndexStatistics
		Public ReadOnly Property ProviderName() As String
			Get
				Return DaveSexton.DocProject.DocSites.DocSiteSearch.DefaultSearchProvider.Name
			End Get
		End Property

		Public ReadOnly Property LastCreationDate() As Nullable(Of DateTime)
			Get
				Return CType(IIf(DaveSexton.DocProject.DocSites.DocSiteSearch.IndexCreated, _
				 CType(DaveSexton.DocProject.DocSites.DocSiteSearch.LastCreationDate.ToLocalTime(), Nullable(Of DateTime)), _
				 Nothing), Nullable(Of DateTime))
			End Get
		End Property

		Public ReadOnly Property KeywordCount() As Integer
			Get
				Return DaveSexton.DocProject.DocSites.DocSiteSearch.KeywordCount
			End Get
		End Property

		Public ReadOnly Property DocumentCount() As Integer
			Get
				Return DaveSexton.DocProject.DocSites.DocSiteSearch.DocumentCount
			End Get
		End Property

		Public ReadOnly Property IndexCreated() As Boolean
			Get
				Return DaveSexton.DocProject.DocSites.DocSiteSearch.IndexCreated
			End Get
		End Property
	End Class
End Class